package net.dfrz.oa.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.dfrz.oa.dao.repository.UserRepository;
import net.dfrz.oa.dto.UserDto;
import net.dfrz.oa.pojo.Role;
import net.dfrz.oa.pojo.User;
import net.dfrz.oa.service.UserService;

/**
 * UserService实现类
 * @author vovh
 *
 */
@Service
@Transactional
public class UserServiceImpl implements UserService{

	@Autowired
	private UserRepository userRepository;
	/**
	 * 登录
	 */
	@Override
	@Transactional
	public User login(User user) {
		String username = user.getUsername();
		String password = user.getPassword();
		return userRepository.getByUsernameAndPassword(username, password);
	}
	
	/**
	 * 查询出所有用户
	 */
	@Override
	@Transactional
	public List<User> findAll() {
		return userRepository.findAll();
	}

	/**
	 * 根据id查询出用户
	 */
	@Override
	public User findById(Integer id) {
		return userRepository.findOne(id);
	}

	/**
	 * 更新用户个人信息
	 */
	@Override
	public void updateUser(UserDto userDto) {
		Integer id = userDto.getUserId();
		String name = userDto.getName();
		String sex = userDto.getSex();
		String major = userDto.getMajor();
		String edu = userDto.getEdu();
		String school = userDto.getSchool();
		userRepository.updateUserInfo(id,name,sex,major,edu,school);
	}

	/**
	 * 修改密码
	 */
	@Override
	public void passwordEdit(Integer uid, String newPwd) {
		userRepository.editPassword(uid,newPwd);
	}

}
